public class MyStringList implements IList{
    public class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    @Override
    //打印列表
    public void display() {

        ListNode cur=head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    public void creatList(){
        ListNode listNode1=new ListNode(12);
        ListNode listNode2=new ListNode(23);
        ListNode listNode3=new ListNode(34);
        ListNode listNode4=new ListNode(45);
        ListNode listNode5=new ListNode(56);

        listNode1.next=listNode2;
        listNode2.next=listNode3;
        listNode3.next=listNode4;
        listNode4.next=listNode5;
        this.head=listNode1;
    }
    @Override
    //列表个数
    public int size() {
        ListNode cur=head;
        int count=0;
        while (cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }
    @Override
    //遍历列表查找
    public boolean contains(int key) {
        ListNode cur=head;
        while (cur!=null){
            if (cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    //头插法
    public void addFirst(int data) {
        ListNode cur =new ListNode(data);
        cur.next=head;
        head=cur;

    }

    @Override
    //尾插法
    public void addLast(int data) {

        ListNode mod=new ListNode(data);
        ListNode cur=head;
        if(head==null){
            head=mod;
            return;
        }
        while (cur.next!=null){
            cur=cur.next;
        }
        cur.next=mod;

    }

    public MyStringList() {
    }


    @Override
    //插入任意位置
    public void addIndex(int index, int data) {
        if(index<0 || index>size()){
            System.out.println("位置不合法");
            return;
        }
        //头插法
        if (index==0){
            addFirst(data);
            return;
        }
        //尾插法
        if(index==size()){
            addLast(data);
            return;
        }

        ListNode cur=head;
        ListNode mod=new ListNode(data);
        while (index-1!=0){
            cur=cur.next;
            index--;
        }
        mod.next=cur.next;
        cur.next=mod;
    }


    @Override
    //删除
    public void remove(int key) {

        if (head==null){
            return;
        }
        if (head.val==key){
            head=head.next;
            return;
        }
        ListNode cur=FindModKey(key);
        if (cur==null){
            return;
        }
        ListNode del=cur.next;
        cur.next=cur.next;
    }
    public ListNode FindModKey(int key){
        ListNode cur=head;
        ListNode mod=new ListNode(key);
        while (cur.next!=null){
            if (cur.next.val==key){
                return cur;
            }
            cur=cur.next;
        }
        return null;
    }

    @Override
    //删除所有key的值
    public void removeAllKey(int key) {
        if (head==null){
            return;
        }

        ListNode prep=head;
        ListNode cur=prep.next;
        while (cur!=null){
            if (cur.val==key){
                prep.next=cur.next;
                cur=cur.next;
            }else {
                prep=cur;
                cur=cur.next;
            }
        }
        if(head.val==key){
            head=head.next;
            return;
        }//放在最后处理(头元素如果==key)

    }



    @Override
    public void clear() {
        ListNode cur=head;
        while (cur!=null){
            ListNode curNext=cur.next;
            cur.next=null;
            cur.next=curNext;
        }

    }
}
